from gasp import *
class Picture:
  def __init__(self, img):
    self.img = img
    self.height = len(img)
    self.width = len(img[0])
    for s in img:
      self.width = max(self.width,len(s))

  def __str__(self):
    return str(self.img)

  def __eq__(self, other):
    return self.img == other.img

  def _invColor(self, color):
    """ Usada internamente para invertir el color de una figura """
    if color == '_':
      return '='
    elif color == '=':
      return '_'
    elif color == '.':
      return '@'
    elif color == '@':
      return '.'
    else:
      return color

  def _overlay(self, c1, c2):
    """ Usada internamente para poner una figura sobre otra """
    if c1 == ' ': 
      return c2
    return c1

#Fin de la clase Picture



def espejoV(pic):
  """ espejoV(Picture) -> Picture
      Devuelve el espejo vertical de la imagen """
  img = pic.img[:]
  for i in range(0, len(img)):
    s = pic.img[i]
    ns = ""
    for c in s:
      ns = c + ns
    img[i] = ns
  return Picture(img)

def espejoH(pic):
  """ espejoH(Picture) -> Picture
      Devuelve el espejo horizontal de la imagen """
  img = pic.img[:]
  for i in range(0, len(img)):
    img[i] = pic.img[len(img) - 1 - i]
  return Picture(img)

def negativo(pic):
  """ negativo(Picture) -> Picture
      Devuelve un negativo de la imagen """
  img = pic.img[:]
  for i in range(0, len(img)):
    s = pic.img[i]
    ns = ""
    for c in s:
      ns += pic._invColor(c)
    img[i] = ns
  return Picture(img)

def alLado(pic, p):
  """ alLado(Picture, Picture) -> Picture
      Devuelve una nueva figura poniendo la primera figura al lado de la 
      segunda """
  img = pic.img[:]
  for i in range(0, len(pic.img)):
    img[i] = pic.img[i] + p.img[i]
  return Picture(img)

def encima(pic, p):
  """ encima(Picture, Picture) -> Picture
      Devuelve una nueva figura poniendo la primera figura encima de la
      segunda """
  img = pic.img[:] + p.img[:]
  return Picture(img)

def superponer(pic, p):
  """ superponer(Picture, Picture) -> Picture
      Devuelve una nueva figura poniendo la primera figura sobre la
      segunda """
  img = pic.img[:]
  for i in range(0, len(pic.img)):
    s = ""
    for j in range(0, len(pic.img[i])):
      s += pic._overlay(pic.img[i][j], p.img[i][j])
    img[i] = s
  return Picture(img)

def repetirH(pic, n):
  """ repetirH(Picture, int) -> Picture
      Devuelve una nueva figura repitiendo la figura actual al costado
      la cantidad de veces que indique el valor de entero"""
  img = pic.img[:]
  for i in range(0, len(img)):
    for j in range(1, n):
      img[i] += pic.img[i]
  return Picture(img)

def repetirV(pic, n):
  """ repetirV(Picture, int) -> Picture
      Devuelve una nueva figura repitiendo la figura actual debajo,
      la cantidad de veces que indique el valor de entero """
  img = pic.img[:]
  for i in range(1, n):
    img += pic.img
  return Picture(img)


#Imagenes fichas de ajedrez

img = ["__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________",
       "__________________________________________________________"]

casilleroBlanco = Picture(img)

img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                            ##                            ",
       "                          ######                          ",
       "                         ###..###                         ",
       "                         ##....##                         ",
       "                         ##....##                         ",
       "                         ###..###                         ",
       "                          ######                          ",
       "                           ####                           ",
       "                         ########                         ",
       "                        ###....###                        ",
       "                      ####......####                      ",
       "                    ####..........####                    ",
       "                   ###..............###                   ",
       "                  ###................###                  ",
       "                 ###..................###                 ",
       "                ###.........##.........###                ",
       "                ##..........##..........##                ",
       "               ###..........##..........###               ",
       "               ##...........##...........##               ",
       "               ##.......##########.......##               ",
       "               ##.......##########.......##               ",
       "               ##...........##...........##               ",
       "               ##...........##...........##               ",
       "               ##...........##...........##               ",
       "               ###..........##..........###               ",
       "                ##..........##..........##                ",
       "                ###....................###                ",
       "                 ##....................###                ",
       "                 ###..................###                 ",
       "                  ###................###                  ",
       "                   ####################                   ",
       "                   ####################                   ",
       "                   ##................##                   ",
       "                  ###................###                  ",
       "                  ##..................##                  ",
       "                  ######################                  ",
       "                 ########################                 ",
       "                 ###..................###                 ",
       "                 #####..............#####                 ",
       "                 ########################                 ",
       "                      ##############                      ",
       "                          ######                          ",
       "                        ####..####                        ",
       "        ##################......##################        ",
       "      ##################..........##################      ",
       "    ####..........................................####    ",
       "    ###.....................##.....................###    ",
       "     ##...................######...................##     ",
       "     ###.########.......####  ####.......########.###     ",
       "      ####################      ####################      ",
       "       ##        #######          #######        ##       ",
       "                                                          ",
       "                                                          ",
       "                                                          "]

alfil = Picture(img)

img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                           ####                           ",
       "                           #..#                           ",
       "                           #..#                           ",
       "                         ###..###                         ",
       "                         #......#                         ",
       "                         ###..###                         ",
       "                           #..#                           ",
       "                           #..#                           ",
       "                           #..#                           ",
       "                           #..#                           ",
       "                           #..#                           ",
       "                          ######                          ",
       "                         ###..###                         ",
       "                         ##....##                         ",
       "                        ###....###                        ",
       "           #######      ##......##      #######           ",
       "         ###########    ##......##    ###########         ",
       "       ####.......####  ##......##  ####.......####       ",
       "      ###...........######......######...........###      ",
       "     ###..............####......####..............###     ",
       "     ##................####....####................##     ",
       "    ###.................###....###.................###    ",
       "    ##...................###..###...................##    ",
       "    ##...................###..###...................##    ",
       "    ##....................######....................##    ",
       "    ##....................######....................##    ",
       "    ##.....................####.....................##    ",
       "    ###....................####....................###    ",
       "     ##.....................##....................###     ",
       "     ###....................##....................##      ",
       "      ###...................##...................###      ",
       "       ###..................##..................###       ",
       "        ###...........##############...........###        ",
       "         ###.....########################.....###         ",
       "          ############..............############          ",
       "           ######........................######           ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##........##############........##            ",
       "            ##...########################...##            ",
       "            ##########..............##########            ",
       "            #####........................#####            ",
       "            ##.........############.........##            ",
       "            ##....######################....##            ",
       "            ##.########............########.##            ",
       "            ######......................######            ",
       "            ######......................######            ",
       "               ########............########               ",
       "                  ######################                  ",
       "                       ############                       ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          "]
rey = Picture(img)

img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "               #         ##                               ",
       "              ###       ####                              ",
       "              #####     ####                              ",
       "               #####   ######                             ",
       "               ##.### ###..##                             ",
       "               ##..#####...######                         ",
       "               ##...####...#########                      ",
       "               ##..#####...##...######                    ",
       "              ###.###.......#.....######                  ",
       "             ###..#.................#####                 ",
       "            ###.......................####                ",
       "            ##.........................####               ",
       "           ###..####....................####              ",
       "           ##..####......................####             ",
       "           ##..####......................#####            ",
       "           ##..###........................####            ",
       "          ###..#..............##...........####           ",
       "          ##..................##...........####           ",
       "          ##..................##............####          ",
       "         ###..................##............####          ",
       "         ##...................##.............####         ",
       "        ###..................###.............####         ",
       "       ###..................###..............####         ",
       "      ###...................###...............####        ",
       "      ##...................####...............####        ",
       "     ###.................######...............####        ",
       "     ##................####  ##................####       ",
       "    ###.##...........####    ##................####       ",
       "    ##.###.........####     ###................####       ",
       "    ##.###........###       ##.................####       ",
       "    ##.##........###       ###.................#####      ",
       "    ##......##..###        ##...................####      ",
       "    ##.....###.###        ###...................####      ",
       "    ###...#######        ###....................####      ",
       "     ########.##        ###.....................####      ",
       "       #########       ###......................####      ",
       "            ###       ###.......................#####     ",
       "                     ###........................#####     ",
       "                    ###..........................####     ",
       "                    ##...........................####     ",
       "                   ###...........................####     ",
       "                  ###............................####     ",
       "                  ##.............................####     ",
       "                 ###.............................####     ",
       "                 ##..............................####     ",
       "                 ##..............................####     ",
       "                 ####################################     ",
       "                 ####################################     ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          "]

caballo = Picture(img)

img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                           ####                           ",
       "                         ########                         ",
       "                        ###....###                        ",
       "                       ###......###                       ",
       "                       ##........##                       ",
       "                       ##........##                       ",
       "                       ##........##                       ",
       "                       ##........##                       ",
       "                       ###......###                       ",
       "                        ###....###                        ",
       "                      #####....#####                      ",
       "                     ###..........###                     ",
       "                    ###............###                    ",
       "                    ##..............##                    ",
       "                   ###..............###                   ",
       "                   ##................##                   ",
       "                   ##................##                   ",
       "                   ##................##                   ",
       "                   ##................##                   ",
       "                   ##................##                   ",
       "                   ##................##                   ",
       "                   ###..............###                   ",
       "                    ##..............##                    ",
       "                    ###............###                    ",
       "                     ####........####                     ",
       "                     ####........####                     ",
       "                   ####............####                   ",
       "                  ###................###                  ",
       "                 ###..................###                 ",
       "                ###....................###                ",
       "               ###......................###               ",
       "               ##........................##               ",
       "              ###........................###              ",
       "              ##..........................##              ",
       "             ###..........................###             ",
       "             ##............................##             ",
       "             ##............................##             ",
       "            ###............................###            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##################################            ",
       "            ##################################            ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          "]

peon = Picture(img)
  
img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                            ##                            ",
       "                          ######                          ",
       "              #####      ###..###      #####              ",
       "             #######     ##....##     #######             ",
       "             ##...##     ##....##     ##...##             ",
       "             ##...##     ########     ##...##             ",
       "    ##       ##...##      ######      ##...##       ##    ",
       "  ######     #######        ##        #######     ######  ",
       " ###..###     #####        ####        #####     ###..### ",
       " ##....##       ###        ####        ###       ##....## ",
       " ###...##       ###        ####        ###       ##...### ",
       "  #######       ###        ####        ###       #######  ",
       "   #####        ####      ######      ####        #####   ",
       "     ###        ####      ##..##      ####        ###     ",
       "     ####       ####      ##..##      ####       ####     ",
       "     ####       #####     ##..##     #####       ####     ",
       "     #####      ##.##     ##..##     ##.##      #####     ",
       "      #####     ##.##    ###..###    ##.##     #####      ",
       "      ##.##    ###.###   ##....##   ###.###    ##.##      ",
       "      ##.###   ##...##   ##....##   ##...##   ###.##      ",
       "      ##..##   ##...###  ##....##  ###...##   ##..##      ",
       "      ##..###  ##....## ###....### ##....##  ###..##      ",
       "      ###..##  ##....## ##......## ##....##  ##..###      ",
       "       ##..### ##....#####......#####....## ###..##       ",
       "       ##...#####.....####......####.....#####...##       ",
       "       ##....####.....####......####.....####....##       ",
       "       ##....####..#..###..####..###..#..####....##       ",
       "       ###....###.######################.###....###       ",
       "        ##.################....################.##        ",
       "        ##########......................##########        ",
       "        ###....................................###        ",
       "         ##....................................##         ",
       "         ###..................................###         ",
       "          ###.......##################.......###          ",
       "           ####################################           ",
       "            ########..................########            ",
       "            ###............................###            ",
       "             ##............................##             ",
       "             ##......################......##             ",
       "             ################################             ",
       "             ########................########             ",
       "             ##............................##             ",
       "            ###.....##################.....###            ",
       "            ##################################            ",
       "           #########..................#########           ",
       "           ##................................##           ",
       "           ##................................##           ",
       "           #########..................#########           ",
       "             ################################             ",
       "                    ##################                    ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          "]

dama = Picture(img)

img = ["                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "           #########     ########     #########           ",
       "           #########     ########     #########           ",
       "           ##.....##     ##....##     ##.....##           ",
       "           ##.....##     ##....##     ##.....##           ",
       "           ##.....#########....#########.....##           ",
       "           ##.....#########....#########.....##           ",
       "           ##................................##           ",
       "           ####################################           ",
       "           ####################################           ",
       "            ###............................###            ",
       "             ####........................####             ",
       "               ############################               ",
       "                ##########################                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##......................##                ",
       "                ##########################                ",
       "               ############################               ",
       "              ###........................###              ",
       "            ##################################            ",
       "            ##################################            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "            ##..............................##            ",
       "        ##########################################        ",
       "        ##########################################        ",
       "        ##......................................##        ",
       "        ##########################################        ",
       "        ##########################################        ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          ",
       "                                                          "]
torre = Picture(img)


#Creacion de la ventana y dibujo de imagen (GASP)

def dibujar(pic):
  begin_graphics(width=pic.width, height=pic.height, title="Ajedrez", background=color.LIGHTBLUE)
  set_speed(0)
  n = len(pic.img)
  for i in range(0, n):
    parseLine(i, pic.img[n - 1 - i]) #desde la ultima fila 
  update_when('key_pressed')
  end_graphics()


#Dibujar lineas 
#Line(start, end, color)

def parseLine(y, s):

  xi = 0
  xf = 1
  c = s[xi]
  while xf < len(s):
    if c == ' ' and c != s[xf]: #end of spaces
      c = s[xf]
      xi = xf
    elif c != s[xf]: #end of pattern
      Line((xi, y), (xf, y), parseChar(c))
      c = s[xf]
      xi = xf
    xf+=1
  if c != ' ':
    Line((xi, y), (xf, y), parseChar(c))

def parseChar(c):
  if c == '_':
    return color.LIGHTGRAY
  elif c == '=':
    return color.GRAY
  elif c == '.':
    return color.WHITE
  elif c == '@':
    return color.BLACK
  elif c == '#':
    return color.DARKGRAY
  else:
    return color.BLUE
  
